/*
定时保存设备数据，如果设备数据发生变化
 */

'use strict';
const _ = require('lodash');
const moment = require('moment');
module.exports = {
  schedule: {
    cron: '0 0 * * * *', // 每小时
    //interval: '1s', // 任务的间隔时间, 这个定义的10秒执行一次
    type: 'worker' // 指定所有的 worker都需要执行
  },
  async task(ctx) {
    const workTimers = await ctx.app.dbs.find('WorkTimer', {});
    // console.log('workTimers', workTimers);
    _.map(workTimers, workTimer => {
      const { isEnabled, timerMethod } = workTimer;
      if (isEnabled && timerMethod === '每天') {
        dayTimer(ctx, workTimer);
      }
      if (isEnabled && timerMethod === '每周') {
        weekTimer(ctx, workTimer);
      }
      if (isEnabled && timerMethod === '每月') {
        mouthTimer(ctx, workTimer);
      }
      if (isEnabled && timerMethod === '每年') {
        yearTimer(ctx, workTimer);
      }
    });
    console.log('-------------------');

  }
};

function dayTimer(ctx, workTimer) {
  const { timerDay } = workTimer;
  const time = moment(timerDay)
    .format('YYYY-MM-DD HH') + ':00';
  const _time = moment(time)
    .format('HH:mm');
  const curTime = moment()
    .format('HH:mm');
  console.log('时间检查', time, _time, curTime);
  if (_time === curTime) {
    console.log('执行', time, _time, curTime);
    createWork(ctx, workTimer);
  }
}

function weekTimer(ctx, workTimer) {
  const { timerDay } = workTimer;
  const shortChinese = moment()
    .format('ddd');
  if (shortChinese === timerDay && moment()
    .format('HH:mm:ss') === '00:00:00') {
    console.log('执行', timerDay, shortChinese);
    createWork(ctx, workTimer);
  }
}

function mouthTimer(ctx, workTimer) {
  const { timerDay } = workTimer;
  const time = moment(timerDay)
    .format('DD');
  const curTime = moment()
    .format('DD');
  console.log('时间检查', time, curTime);
  if (time === curTime && moment()
    .format('HH:mm:ss') === '00:00:00') {
    console.log('执行', time, curTime);
    createWork(ctx, workTimer);
  }
}

function yearTimer(ctx, workTimer) {
  const { timerDay } = workTimer;
  const time = moment(timerDay)
    .format('MM-DD');
  const curTime = moment()
    .format('MM-DD');
  console.log('时间检查', time, curTime);
  if (time === curTime && moment()
    .format('HH:mm:ss') === '00:00:00') {
    console.log('执行', time, curTime);
    createWork(ctx, workTimer);
  }
}

function createWork(ctx, workTimer) {
  const { service } = ctx;
  const { delayDay, workList = [] } = workTimer;
  const requiredCompletionDate = moment()
    .add(delayDay - 1, 'days');
  workTimer.code = 'WRK' + moment()
    .format('YYYYMMDDHHmmss');
  workTimer.requiredCompletionDate = requiredCompletionDate;
  _.map(workList, it => {
    it.code = 'TSK' + moment()
      .format('YYYYMMDDHHmmss');
    it.requiredCompletionDate = requiredCompletionDate;
  });
  delete workTimer._id;
  workTimer.isTimerWork = true;
  service['workService'].createWork(workTimer);
}

/**
 * {
 *   code: 'WRK20251106144122',
 *   workName: '任务一',
 *   desc: '详细内容1',
 *   requiredCompletionDate: '2025-11-21T06:41:31.051Z',
 *   punishAmount: 12,
 *   rewardAmount: 22,
 *   workList: [
 *     {
 *       code: 'TSK20251106144151',
 *       username: '张三',
 *       offlineMan: 'xiaobing',
 *       desc: '详细内容123',
 *       requiredCompletionDate: '2025-11-21T06:41:31.051Z',
 *       userCode: 'HM2510300050474A',
 *       workCode: 'WRK20251106144122',
 *       adminCode: 'WRK20251106144122',
 *       createUsername: 'chke'
 *     }
 *   ],
 *   isAdmin: true,
 *   tenantCode: 'TEN2510300049342E',
 *   createUser: 'chke',
 *   createUsername: '陈科',
 *   tenantName: '镇江新蓝化工'
 * }
 */
